package com.sbl.bajw.pc.action;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sbl.bajw.common.ExcelUtil;
import com.sbl.bajw.common.web.JsonResultUtil;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.common.web.SessionUnit;
import com.sbl.bajw.common.web.WebContext;
import com.sbl.bajw.pc.model.CameraHouseModel;
import com.sbl.bajw.pc.model.CameraModel;
import com.sbl.bajw.pc.model.LogTModel;
import com.sbl.bajw.pc.service.CameraService;
import com.sbl.bajw.pc.service.DictModelService;
import com.sbl.bajw.pc.service.LogTModelService;
import com.sbl.bajw.util.DateUtil;
import com.sbl.bajw.util.JsonUtil;

@Controller
@RequestMapping("/Camera")
public class CameraAction {
	
	@Resource
	private CameraService service;
	
	@Resource
	private DictModelService dictService;
	
	@Resource
	private LogTModelService logService;
	
	/**
	 * 摄像头采集数据简易搜索列表查询
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getEasyList")
	public @ResponseBody String getEasyList(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		parm.put("user_id", SessionUnit.getUserId());
		String json = service.getEasyList(parm);
		return json;
	}
	
	@RequestMapping(value = "/testParm")
	public @ResponseBody String testParm() throws UnsupportedEncodingException{
		HttpServletRequest request = WebContext.getRequest();
		request.setCharacterEncoding("UTF-8");
		String parm = request.getParameter("personId");
		System.out.println(RequestUnit.getString("personId"));
		System.out.println(parm);
		return parm;
	}
	
	/**
	 * 摄像头采集数据列表查询
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getlist")
	public @ResponseBody String getlist(HttpServletRequest request, HttpServletResponse response){

		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		parm.put("user_id", SessionUnit.getUserId());
		String result = service.getList(parm);
		return result;
	} 
	
	/**
	 * 导出摄像头采集数据位EXCEL文件
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value = "/download")
	public void download(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		parm.put("user_id", SessionUnit.getUserId());
		
		List<CameraModel> list = service.download(parm);
		for(int i = 0; i < list.size(); i++){
			if("0".equals(list.get(i).getIsQuelity())){
				list.get(i).setIsQuelity("未审核");
			}else if("1".equals(list.get(i).getIsQuelity())){
				list.get(i).setIsQuelity("审核通过");
			}else if("-1".equals(list.get(i).getIsQuelity())){
				list.get(i).setIsQuelity("审核不通过");
			}else if("2".equals(list.get(i).getIsQuelity())){
				list.get(i).setIsQuelity("提交");
			}
			if("0".equals(list.get(i).getIndoorOutdoor())){
				list.get(i).setIndoorOutdoor("室外");
			}else if("1".equals(list.get(i).getIndoorOutdoor())){
				list.get(i).setIndoorOutdoor("室内");
			}
		}
		LinkedHashMap<String, String> column = new LinkedHashMap<String, String>();
		column.put("lon", "经度");
		column.put("lat", "维度");
		column.put("belongName", "摄像头所有人或单位");
		column.put("belongPhone", "所有人或单位电话号码");
		column.put("savetime", "视频保存期限");
		column.put("address", "监控室位置详址");
		column.put("factoryName", "监控设备生产厂商");
		column.put("typename", "摄像头类别");
		column.put("directionname", "摄像头方向");
		column.put("classname", "摄像头类型");
		column.put("cameraAddress", "摄像头位置详址");
		column.put("atmCode", "ATM取款机编号");
		column.put("imgurl", "摄像头安装环境照片");
		column.put("yunyingshangname", "基站运营商");
		column.put("baseStationName", "基站名称");
		column.put("baseStationCode", "基站代码");
		column.put("indoorOutdoor", "室内室外");
		column.put("creatername", "采集人");
		column.put("createtime", "采集时间");
		column.put("placename", "摄像头场所分类");
		column.put("buildingCode", "楼栋编码");
		column.put("note", "备注");
		column.put("isQuelity", "是否合格");
		column.put("owner", "建设单位（机主）");
		column.put("maintenanceName", "维护人（负责人）");
		column.put("telNumber", "联系电话");
		column.put("auditusername", "审核人");
		column.put("audituserno", "采集人账号");
		column.put("auditusercardid", "采集人身份证号");
		column.put("audittime", "审核时间");
		column.put("auditreason", "审核原因");
		column.put("deptname", "摄像头所在单位");
		column.put("installDate", "安装时间");
		column.put("camerahousename", "监控室名称");
		column.put("chpscname", "监控室所在派出所名称");
		column.put("chsqname", "监控室所在社区名称");
		column.put("createrdepaname", "采集人单位");
		HSSFWorkbook wb = ExcelUtil.createMExcel(list, column);
		OutputStream outStream = response.getOutputStream();
		response.setContentType("application/x-msdownload");  
		response.setHeader("Content-Disposition","attachment;filename=Camera.xls");  
		wb.write(outStream);
	}
	
	/**
	 * 简易菜单跳转
	 * @作者：罗琦
	 * @return
	 */
	@RequestMapping(value = "/easyList")
	public ModelAndView easyList(){
		ModelAndView mv = new ModelAndView("/view/manager/camera/EasyList");
		return mv;
	}
	
	/**
	 * 菜单跳转
	 * @作者：罗琦
	 * @return
	 */
	@RequestMapping(value = "/List")
	public ModelAndView list(){
		List<HashMap<String, Object>> CameraClassDict = dictService.getCameraClassDict();
		List<HashMap<String, Object>> CameraDirectionDict = dictService.getCameraDirectionDict();
		List<HashMap<String, Object>> CameraPlaceDict = dictService.getCameraPlaceDict();
		List<HashMap<String, Object>> CameraTimeDict = dictService.getCameraTimeDict();
		List<HashMap<String, Object>> CameraTypeDict = dictService.getCameraTypeDict();
		List<HashMap<String, Object>> CameraYunyingshangDict = dictService.getCameraYunyingshangDict();
		
		RequestUnit.setAttr("CameraClassDict", CameraClassDict);
		RequestUnit.setAttr("CameraDirectionDict", CameraDirectionDict);
		RequestUnit.setAttr("CameraPlaceDict", CameraPlaceDict);
		RequestUnit.setAttr("CameraTimeDict", CameraTimeDict);
		RequestUnit.setAttr("CameraTypeDict", CameraTypeDict);
		RequestUnit.setAttr("CameraYunyingshangDict", CameraYunyingshangDict);
		
		ModelAndView mv = new ModelAndView("/view/manager/camera/List");
		return mv;
	}
	
	@RequestMapping(value = "/detail")
	public ModelAndView detail(){
		String id = RequestUnit.getString("id");
		List<HashMap<String, String>> pics = service.getPics(id);
		CameraModel model = service.getModel(id);
		RequestUnit.setAttr("model", model);
		RequestUnit.setAttr("pics", pics);
		
		ModelAndView mv = new ModelAndView("/view/manager/camera/detail");
		return mv;
	}
	
	/**
	 * APP摄像头详情查询
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "detailApp")
	public @ResponseBody String detailApp(HttpServletRequest request, HttpServletResponse response){
		String id = RequestUnit.getString("id");
		
		List<HashMap<String, String>> pics = service.getPics(id);
		CameraModel model = service.getModel(id);
		
		String result = "{\"image\":" + JsonUtil.ConvertArrayToJson(pics) + ",\"data\":" + JsonUtil.ConvertObjectToJson(model) + "}";
		return JsonResultUtil.Success("", result);
	}
	
	@RequestMapping(value = "/detailInMap")
	public ModelAndView detailInMap(){
		String id = RequestUnit.getString("id");
		List<HashMap<String, String>> pics = service.getPics(id);
		CameraModel model = service.getModel(id);
		RequestUnit.setAttr("model", model);
		RequestUnit.setAttr("pics", pics);
		
		ModelAndView mv = new ModelAndView("/view/manager/camera/detailInMap");
		return mv;
	}
	
	@RequestMapping(value = "/edit")
	public ModelAndView edit(){
		String id = RequestUnit.getString("id");
		
		List<HashMap<String, Object>> CameraClassDict = dictService.getCameraClassDict();
		List<HashMap<String, Object>> CameraDirectionDict = dictService.getCameraDirectionDict();
		List<HashMap<String, Object>> CameraPlaceDict = dictService.getCameraPlaceDict();
		List<HashMap<String, Object>> CameraTimeDict = dictService.getCameraTimeDict();
		List<HashMap<String, Object>> CameraTypeDict = dictService.getCameraTypeDict();
		List<HashMap<String, Object>> CameraYunyingshangDict = dictService.getCameraYunyingshangDict();
		List<HashMap<String, String>> pics = service.getPics(id);
		
		RequestUnit.setAttr("CameraClassDict", CameraClassDict);
		RequestUnit.setAttr("CameraDirectionDict", CameraDirectionDict);
		RequestUnit.setAttr("CameraPlaceDict", CameraPlaceDict);
		RequestUnit.setAttr("CameraTimeDict", CameraTimeDict);
		RequestUnit.setAttr("CameraTypeDict", CameraTypeDict);
		RequestUnit.setAttr("CameraYunyingshangDict", CameraYunyingshangDict);
		
		CameraModel model = service.getModel(id);
		RequestUnit.setAttr("model", model);
		RequestUnit.setAttr("pics", pics);
		
		ModelAndView mv = new ModelAndView("/view/manager/camera/main");
		return mv;
	}
	
	/**
	 * 摄像头采集数据入库
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return 
	 * @throws Exception 
	 */
	@RequestMapping(value = "/createApp")
	public @ResponseBody String createApp(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String userId = RequestUnit.getString("user_id");
		CameraModel model = RequestUnit.getModel(CameraModel.class);
		model.setCreater(userId);
		model.setIsQuelity("0");
		CameraHouseModel chModel = service.insert(model);

		LogTModel logT = new LogTModel();
		logT.setUserid(userId);
		logT.setCode("14");
		logService.insert(logT);
		
		String json = JsonUtil.ConvertObjectToJson(chModel);
		return JsonResultUtil.Success(json);
	}

	@RequestMapping(value = "/update")
	public @ResponseBody String update() throws MessageException{
		CameraModel model = RequestUnit.getModel(CameraModel.class);
		model.setUpdater(SessionUnit.getUserId());
		service.update(model);
		String json = JsonResultUtil.Success();
		return json;
	}
	
	/**
	 * APP更新摄像头信息
	 * @作者：罗琦
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/updateApp")
	public @ResponseBody String updateApp() throws Exception{
		CameraModel model = RequestUnit.getModel(CameraModel.class);
		String userId = RequestUnit.getString("user_id");
		model.setUpdater(userId);
		service.updateApp(model);
		String json = JsonResultUtil.Success();
		return json;
	}
	
	@RequestMapping(value = "/mapView")
	public ModelAndView mapView(){
		List<CameraModel> models = service.getModelsByIds();
		RequestUnit.setAttr("models", models);
		ModelAndView mv = new ModelAndView("/view/manager/camera/mapView");
		return mv;
	}
	
	/**
	 * 统计总数、每日采集数、未审核总数以及审核通过总数
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getStatisticOnDay")
	public @ResponseBody String getStatisticOnDay(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> map = service.getStatisticOnDay();
		String json = JsonUtil.ConvertObjectToJson(map);
		return json;
	}
	
	/**
	 * APP查询摄像头列表
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getListApp")
	public @ResponseBody String getListApp(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String start = DateUtil.getMondayDate();
		String stop = DateUtil.getSundayDate();
		parm.put("startdate", start + " 00:00:00");
		parm.put("stopdate", stop + "23:59:59");
		
		List<HashMap<String, Object>> list = service.getListApp(parm);
		String json = JsonResultUtil.Success(JsonUtil.ConvertArrayToJson(list));
//		int num = service.getCountApp(parm);
//		String result = "{\"num\":" + num + ",\"list\":" + JsonUtil.ConvertArrayToJson(list) + "}";
//		String json = JsonResultUtil.Success("", JsonUtil.ConvertObjectToJson(result));
		return json;
	}

	@RequestMapping(value = "/getCenterCount")
	public @ResponseBody String getCenterCount(HttpServletRequest request, HttpServletResponse response){
		
		HashMap<String, String> parm = RequestUnit.getHashMapParmS();
		
		List<HashMap<String, String>> list = service.getCenterCount(parm);
		String json = JsonResultUtil.Success("", JsonUtil.ConvertArrayToJson(list));
		return json;
	}
	
	@RequestMapping(value = "/getModelsForMap")
	public @ResponseBody String getModelsForMap(HttpServletRequest request, HttpServletResponse response){
		
		HashMap<String, String> parm = RequestUnit.getHashMapParmS();
		
		List<HashMap<String, String>> list = service.getModelsForMap(parm);
		String json = JsonResultUtil.Success("", JsonUtil.ConvertArrayToJson(list));
		return json;
	}
}
